package com.wg.db;

import com.wg.core.properties.PropertiesMgr;
import com.wg.db.ds.DataSourceConfig;

import java.util.Map;

/**
 * @author 少爷123
 */
public class DbConfig {

    private final Map<String, DataSourceConfig> dataSourceConfigMap;

    /**
     * 数据回写间隔(秒)
     */
    private final int writeInterval;

    /**
     * 数据一次写入条数
     */
    private final int writeMaxSize;

    public DbConfig() {
        dataSourceConfigMap = PropertiesMgr.toMap(DataSourceConfig.class, DbConstant.DATA_SOURCE_PREFIX);
        writeInterval = Math.max(PropertiesMgr.getInt(DbConstant.DATA_WRITE_INTERVAL, 0), 1);
        writeMaxSize = Math.max(PropertiesMgr.getInt(DbConstant.DATA_WRITE_MAX_SIZE, 0), 100);
    }

    public static DbConfig getInstance() {
        return Singleton.INSTANCE.getManager();
    }

    public int getWriteInterval() {
        return writeInterval;
    }

    public int getWriteMaxSize() {
        return writeMaxSize;
    }

    public Map<String, DataSourceConfig> getDataSourceConfigMap() {
        return dataSourceConfigMap;
    }

    private enum Singleton {
        INSTANCE;

        final DbConfig manager;

        Singleton() {
            this.manager = new DbConfig();
        }

        public DbConfig getManager() {
            return manager;
        }
    }
}
